package com.wondersgroup.insurance.dnuilearn.views.xformode;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/5/15.
 */
/*http://blog.csdn.net/aishang5wpj/article/details/50624101

先画mDstB

        然后设置xfermode

        最后画mSrc*/
public class CircleImageView  extends View {
    Bitmap mBitmap, dstBmp;
    int centerX, centerY;

    int radius;
    Paint mPaint;


    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);

        mPaint = new Paint();
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy2);


        radius = Math.min(mBitmap.getWidth(),mBitmap.getHeight())/2;

        dstBmp = Bitmap.createBitmap(mBitmap.getWidth(),mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(dstBmp);
        mPaint.setColor(Color.RED);
        canvas.drawCircle(radius,radius,radius,mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);



    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.drawBitmap(dstBmp,0,0,mPaint);
        mPaint.setColor(Color.RED);

        canvas.drawCircle(radius,radius,radius,mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN  ));
//        mPaint.setColor(Color.RED);
//        canvas.drawCircle(centerX,centerY,radius,mPaint);

        canvas.drawBitmap(mBitmap,0,0,mPaint);


        mPaint.setXfermode(null);
    }
}
